<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/5/12
 * Time: 16:34
 */

namespace Admin\Controller;
use Think\Controller;

class ManageController extends CommonController
{

    private $admin='';

    function __construct()
    {
        parent::__construct();
        $this->admin=D('Admin');
        $this->admin->startTrans();
    }

    function index()
    {
        if($_POST){
            $data=$_POST;
            $data['id']='';
            $data['add_time']=time();

            $yanG1=implode(";",$_POST['auth']);
            $data['auth']=$yanG1;
            $aa=$this->admin->data($data)->add();
            if($aa==true)
            {
                $this->admin->commit();
                $this->success("人员添加成功");
            }else{
                $this->admin->rollback();
                $this->error("人员添加失败");
            }
        }else{
            $arr=$this->admin->select();
            $data=$arr;
            foreach($arr as $key=>$r)
            {
                $data[$key]['add_time']=date("Y-m-d",$r['add_time']);
            }
            $this->assign("data",$data);
            $this->display();
        }
    }

    function del($id='')
    {
        $aa=$this->admin->where("id=".$id)->delete();
        if($aa==true)
        {
            $this->admin->commit();
            $this->success("人员删除成功");
        }else{
            $this->admin->rollback();
            $this->error("人员删除失败");
        }
    }

    function updata($id='')
    {
        if($_POST)
        {
            $data=$_POST;
            $yanG1=implode(";",$_POST['auth']);
            $data['auth']=$yanG1;
            $aa=$this->admin->where("id=".$id)->save($data);
            if($aa==true)
            {
                $this->admin->commit();
                $this->success("人员信息修改成功");
            }else{
                $this->admin->rollback();
                $this->error("人员信息修改失败");
            }
        }else{
            $yanG1=$this->admin->where("id=".$id)->find();

            $yanGAuth=explode(";",$yanG1['auth']);

            $this->assign("dataAuth",$yanGAuth);

            $this->assign("data",$yanG1);

            $this->display();
        }
    }

}